package com.naizhuai.ui.user;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.naizhuai.AppContext;
import com.naizhuai.Model.AccountHelper;
import com.naizhuai.R;
import com.naizhuai.network.NetWorkObserver;
import com.naizhuai.network.apistore.apistoreiml.CommonManagerIml;
import com.naizhuai.ui.BaseActivity;
import com.naizhuai.ui.components.DialogSureCancel;
import com.naizhuai.ui.components.swipeback.SwipeBackActivity;
import com.naizhuai.ui.entity.JumpBean;
import com.naizhuai.ui.mianbusiness.setting.ApkDownQRCodeActivity;
import com.naizhuai.ui.mianbusiness.setting.SuggestActivity;
import com.naizhuai.ui.user.login.LoginActivity;
import com.naizhuai.ui.user.simulation.SimulationSettingActivity;
import com.naizhuai.ui.user.userinfo.UserDetialActivity;
import com.naizhuai.util.DialogUtils;
import com.naizhuai.util.NotifyUtil;
import com.naizhuai.util.PacageUtils;
import com.tencent.bugly.beta.Beta;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;

public class InfoSettingActivity extends SwipeBackActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;

    public static void start(Context context) {
        Intent intent = new Intent(context, InfoSettingActivity.class);
        if (context instanceof BaseActivity)
            ((BaseActivity) context).startActivityTransition(intent);
    }

    @BindView(R.id.versionName)
    TextView versionName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        versionName.setText(PacageUtils.getVersionName(this));
        toolbar.setNavigationOnClickListener(v -> finish());
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_info_setting;
    }

    @OnClick(R.id.person_info_editor)
    public void onPersonInfoEditorClicked() {
        UserDetialActivity.start(this);
    }

    @OnClick(R.id.version_update)
    public void versionUpdate() {
        Beta.checkUpgrade(false, false);
    }

    @OnClick(R.id.update_password)
    public void updatePassword() {
        UpdatePasswordActivity.start(this);
    }

    @OnClick(R.id.exit_login)
    public void exitLogin() {
//        DialogSureCancel sureCancel = new DialogSureCancel(this);
//        sureCancel.getTvCancel().setOnClickListener(v -> sureCancel.cancel());
//        sureCancel.getTvSure().setOnClickListener(v ->{
//            sureCancel.cancel();
//
//
//        });
//        sureCancel.show();
        DialogUtils.showSureDialog(this, getString(R.string.is_sure_logout), v -> {
            CommonManagerIml.getInstance().user_report(JPushInterface.getRegistrationID(this), JumpBean.logout)
                    .subscribe(new NetWorkObserver<String>() {
                        @Override
                        protected void onFail(String errormsg) {
                        }

                        @Override
                        protected void onSuccess(String result) {

                        }
                    });
            LoginActivity.exitLoginstart(getActivityContext());
        });
    }

    @OnClick(R.id.simulation_setting)
    public void onSimulationSettingClicked() {
        if (isComPanyPower()){
            SimulationSettingActivity.start(this);
        }else {
            DialogUtils.showPromptDialog(this,getString(R.string.noPowerSeeData));
        }

    }

    private int requestCode = (int) SystemClock.uptimeMillis();

    private void sendNotify() {
        Intent intent = new Intent(this, InfoSettingActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pIntent = PendingIntent.getActivity(this,
                requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        int smallIcon = R.drawable.small_logo;
        String ticker = "您有一条新通知";
        String title = "人员添加申请";
        String content = "XXX请求加入公司XXXXXX部门";
        NotifyUtil notify = new NotifyUtil(this, 1);
        notify.notify_normal_singline(pIntent, smallIcon, ticker, title, content,
                true, true, false);
    }

    @OnClick(R.id.submit_advise)
    public void onSubmitAdviseClicked() {
        Intent intent = new Intent(this, SuggestActivity.class);
        startActivityTransition(intent);
    }

    @OnClick(R.id.apk_download)
    public void onGetApkDownloadClick() {
        ApkDownQRCodeActivity.startActivity(this);
    }
}
